<?php

namespace App\Http\Controllers\Admin;

use App\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    public function add()
    {
        return view('admin.user.add');
    }
    public function insert(Request $request)
    {
        $this->validate($request, [
            'username'=>'required|regex:/\w{6,20}/',
            'email'=>'required|email',
            'passworld'=>'same:repassworld',
        ], [
            'username.required'=>'用户名不能省略',
            'username.regex'=>'用户名不合法,请填写6到20位字母数字或下划线',
            'email.required'=>'邮箱不能为空',
            'email.email'=>'邮箱格式不正确',
            'passworld.same'=>'2次密码不一致',
        ]);
        //数据插入
        $user = new User;
        $user->username = $request->input('username');
        $user->email = $request->input('email');
        $user->passworld = Hash::make($request->input('passworld'));
        $user->intro = $request->input('intro');
        //处理图片上传
        if($request->hasFile('profile')){

            $path = './uploads/'.date('Ymd');

            //获取文件后缀名
            $suffix = $request->file('profile')->getClientOriginalExtension();

            $filename = time().rand(100000,900000).'.'.$suffix;

            $request->file('profile')->move($path, $filename);

        }
        $user->profile = trim($path.'/'.$filename, '.');
        $user->date = time();

        //执行插入
        if($user->save()){
            return redirect('user/index')->with('info', '添加成功');
        }else{
            return back()->with('info', '添加失败');
        }

    }
    public function index(Request $request)
    {

        //数据分页显示
        $keyworld = $request->input('keyword');
        if(empty($keyworld)){
            $users = User::orderBy('id', 'desc')->paginate($request->input('num', 10));
        }else{
            $users = User::orderBy('id', 'desc')
                ->where('username','like','%'.$keyworld.'%')
                ->paginate($request->input('num', 10));
        }


        return view('admin.user.index', ['users'=>$users, 'request'=>$request]);
    }
    public function edit( $id )
    {
        $user = User::findOrFail( $id );

        return view('admin.user.edit', ['user'=>$user] );
    }

    public function update( Request $request )
    {
        $user = User::findOrFail( $request->input('id') );
        $user->username = $request->input('username');
        $user->email = $request->input('email');
        $user->intro = $request->input('intro');
        //处理图片上传
        if($request->hasFile('profile')){

            $path = './uploads/'.date('Ymd');

            //获取文件后缀名
            $suffix = $request->file('profile')->getClientOriginalExtension();

            $filename = time().rand(100000,900000).'.'.$suffix;

            $request->file('profile')->move($path, $filename);

        }
        $user->profile = trim($path.'/'.$filename, '.');

        if($user->save()){
            return redirect('admin/user/index')->with('info', '更新成功');
        }else{
            return back()->with('info', '更新失败');
        }
    }

    public function delete( $id )
    {
        $user = User::findOrFail($id);
        $profile = $user->profile;
        $path = '.'.$profile;
        if(file_exists($path)){
            unlink($path);
        }

        if($user->delete()){
            return back()->with('info', '删除成功');
        }else{
            return back()->with('info', '删除失败');
        }
    }
}
